// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
const vscode = require('vscode');

// this method is called when your extension is activated
// your extension is activated the very first time the command is executed

/**
 * @param {vscode.ExtensionContext} context
 */
function activate(context) {
	//转换大写
	let disposableToUpperCase = vscode.commands.registerCommand('extension.toUpperCase', function () {
		let text = getSelectText();
		if(text.code == 0) {
			replaceText(text.text.toUpperCase());
		}
	});
	
	//转换小写
	let disposableToLowerCase = vscode.commands.registerCommand('extension.toLowerCase', function () {
		let text = getSelectText();
		if(text.code == 0) {
			replaceText(text.text.toLowerCase());
		}
	});

	context.subscriptions.push(disposableToUpperCase);
	context.subscriptions.push(disposableToLowerCase);
}
exports.activate = activate;

// this method is called when your extension is deactivated
function deactivate() {
	console.log("your excention deactive");
}

function getSelectText() {
	let editor = vscode.window.activeTextEditor;
	if (!editor.selection.isEmpty) {
		let selection = editor.selection;
		let text = editor.document.getText(selection);
		return {code:0, text:text};
	}
	return {code:1, text:null};
}

function replaceText(newText) {
	let editor = vscode.window.activeTextEditor;
	let start = editor.selection.start;
	let end = editor.selection.end;
	vscode.window.activeTextEditor.edit(editBuilder => {
		editBuilder.replace(new vscode.Range(start, end), newText);
	});

}

module.exports = {
	activate,
	deactivate
}
